package com.xyht.sca_s.student_manage_system.modules.appDynamic.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.req.AppConditionListReq;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.resp.ConditionNameResp;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.resp.ConditionalControlNameResp;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.mapper.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.service.AppConditionListService;
import com.xyht.sca_s.student_manage_system.modules.meeting.entity.req.CheckboxReq;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.appDynamic.constant.ConditionControlSubTypeConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.appDynamic.constant.ControlTypeConstant.*;

/**
 * <p>
 * 条件展示表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-09-06
 */
@Service
public class AppConditionListServiceImpl extends ServiceImpl<AppConditionListMapper, AppConditionList> implements AppConditionListService {
    @Resource
    private AppConditionListMapper appConditionListMapper;
    @Resource
    private AppBasicInfoMapper appBasicInfoMapper;
    @Resource
    private AppControlOrderMapper appControlOrderMapper;
    @Resource
    private AppTextControlConfigMapper appTextControlConfigMapper;
    @Resource
    private AppTimeControlConfigMapper appTimeControlConfigMapper;
    @Resource
    private AppOptionControlConfigMapper appOptionControlConfigMapper;
    @Resource
    private AppStepperControlConfigMapper appStepperControlConfigMapper;
    @Resource
    private AppFileControlConfigMapper appFileControlConfigMapper;

    @Override
    public ResponseResult addAppConditionList(AppConditionListReq conditionListReq) {
        if (isNullOrEmpty(conditionListReq.getSort(), conditionListReq.getConditionName(), conditionListReq.getControlSubType())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        AppConditionList appConditionList = new AppConditionList();
        BeanUtils.copyProperties(conditionListReq, appConditionList);

        appConditionListMapper.insert(appConditionList);

        if (isNullOrEmpty(appConditionList.getId())) {
            return CommonResult.failed(CommonCodeEnum.APP_CONDITION_LIST_INSERT_FAIL);
        }

        return CommonResult.success();
    }

    @Override
    public ResponseResult displayConditionalControl(String appId) {
        //校验应用是否存在
        AppBasicInfo appBasicInfo = appBasicInfoMapper.selectById(appId);
        if (isNullOrEmpty(appBasicInfo)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //查询控件是否存在
        List<AppControlOrder> controlOrderList = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                .eq(AppControlOrder::getAppId, appId));
        if (isNullOrEmpty(controlOrderList) || controlOrderList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.CONTROL_NOT_EXIST);
        }

        //构造返回数据(可展示条件)
        ArrayList<ConditionalControlNameResp> conditionalControlNameRespList = new ArrayList<>();
        for (AppControlOrder appControlOrder : controlOrderList) {
            String type = appControlOrder.getType();
            ConditionalControlNameResp nameResp = new ConditionalControlNameResp();
            nameResp.setSort(appControlOrder.getSort());
            nameResp.setControlOrderId(appControlOrder.getId());
            if (Arrays.asList(TextControlType).contains(type)) {
                AppTextControlConfig textControlConfig = appTextControlConfigMapper.selectTextControlConfig(appControlOrder.getId());
                //控件不存在
                if (isNullOrEmpty(textControlConfig)) {
                    return CommonResult.failed(CommonCodeEnum.CONTROL_NOT_EXIST);
                }
                //控件必传
                if (textControlConfig.getRequired()) {
                    //可作为条件
                    if (Arrays.asList(ConditionControlSubTypeStrArr).contains(textControlConfig.getSubType())) {
                        nameResp.setName(textControlConfig.getName());
                        nameResp.setSubType(textControlConfig.getSubType());
                        //设置条件
                        List<AppConditionList> appConditionList = appConditionListMapper.selectList(new LambdaQueryWrapper<AppConditionList>()
                                .eq(AppConditionList::getControlSubType, textControlConfig.getSubType()));
                        List<ConditionNameResp> collect = appConditionList.stream().map(appCondition -> {
                            ConditionNameResp conditionNameResp = new ConditionNameResp();
                            BeanUtils.copyProperties(appCondition, conditionNameResp);
                            return conditionNameResp;
                        }).collect(Collectors.toList());
                        nameResp.setConditionList(collect);

                        conditionalControlNameRespList.add(nameResp);
                    }
                }
            } else if (Arrays.asList(TimeControlType).contains(type)) {
                AppTimeControlConfig timeControlConfig = appTimeControlConfigMapper.selectTimeControlConfig(appControlOrder.getId());
                //控件不存在
                if (isNullOrEmpty(timeControlConfig)) {
                    return CommonResult.failed(CommonCodeEnum.CONTROL_NOT_EXIST);
                }
                //控件必传
                if (timeControlConfig.getRequired()) {
                    //可作为条件
                    if (Arrays.asList(ConditionControlSubTypeStrArr).contains(timeControlConfig.getSubType())) {
                        nameResp.setName(timeControlConfig.getName());
                        nameResp.setSubType(timeControlConfig.getSubType());
                        //设置条件
                        List<AppConditionList> appConditionList = appConditionListMapper.selectList(new LambdaQueryWrapper<AppConditionList>()
                                .eq(AppConditionList::getControlSubType, timeControlConfig.getSubType()));
                        List<ConditionNameResp> collect = appConditionList.stream().map(appCondition -> {
                            ConditionNameResp conditionNameResp = new ConditionNameResp();
                            BeanUtils.copyProperties(appCondition, conditionNameResp);
                            return conditionNameResp;
                        }).collect(Collectors.toList());
                        nameResp.setConditionList(collect);

                        conditionalControlNameRespList.add(nameResp);
                    }
                }
            } else if (Arrays.asList(OptionControlType).contains(type)) {
                AppOptionControlConfig optionControlConfig = appOptionControlConfigMapper.selectOptionControlConfig(appControlOrder.getId());
                //控件不存在
                if (isNullOrEmpty(optionControlConfig)) {
                    return CommonResult.failed(CommonCodeEnum.CONTROL_NOT_EXIST);
                }
                //控件必传
                if (optionControlConfig.getRequired()) {
                    //可作为条件
                    if (Arrays.asList(ConditionControlSubTypeStrArr).contains(optionControlConfig.getSubType())) {
                        nameResp.setName(optionControlConfig.getName());
                        nameResp.setSubType(optionControlConfig.getSubType());
                        //设置条件
                        List<AppConditionList> appConditionList = appConditionListMapper.selectList(new LambdaQueryWrapper<AppConditionList>()
                                .eq(AppConditionList::getControlSubType, optionControlConfig.getSubType()));
                        List<ConditionNameResp> collect = appConditionList.stream().map(appCondition -> {
                            ConditionNameResp conditionNameResp = new ConditionNameResp();
                            BeanUtils.copyProperties(appCondition, conditionNameResp);
                            return conditionNameResp;
                        }).collect(Collectors.toList());
                        nameResp.setConditionList(collect);
                        //设置选项值
                        if (!optionControlConfig.getSubType().equals(CONDITION_SUBTYPE_SWITCH)) {
                            List<CheckboxReq> checkboxReqList = JSON.parseArray(optionControlConfig.getCheckboxList(), CheckboxReq.class);
                            nameResp.setCheckboxList(checkboxReqList);
                        }

                        conditionalControlNameRespList.add(nameResp);
                    }
                }
            } else if (Arrays.asList(StepperControlType).contains(type)) {
                AppStepperControlConfig stepperControlConfig = appStepperControlConfigMapper.selectStepperControlConfig(appControlOrder.getId());
                //控件不存在
                if (isNullOrEmpty(stepperControlConfig)) {
                    return CommonResult.failed(CommonCodeEnum.CONTROL_NOT_EXIST);
                }
                //控件必传
                if (stepperControlConfig.getRequired()) {
                    //可作为条件
                    if (Arrays.asList(ConditionControlSubTypeStrArr).contains(stepperControlConfig.getSubType())) {
                        nameResp.setName(stepperControlConfig.getName());
                        nameResp.setSubType(stepperControlConfig.getSubType());
                        //设置条件
                        List<AppConditionList> appConditionList = appConditionListMapper.selectList(new LambdaQueryWrapper<AppConditionList>()
                                .eq(AppConditionList::getControlSubType, stepperControlConfig.getSubType()));
                        List<ConditionNameResp> collect = appConditionList.stream().map(appCondition -> {
                            ConditionNameResp conditionNameResp = new ConditionNameResp();
                            BeanUtils.copyProperties(appCondition, conditionNameResp);
                            return conditionNameResp;
                        }).collect(Collectors.toList());
                        nameResp.setConditionList(collect);

                        conditionalControlNameRespList.add(nameResp);
                    }
                }
            } else if (Arrays.asList(FileControlType).contains(type)) {
                AppFileControlConfig fileControlConfig = appFileControlConfigMapper.selectFileControlConfig(appControlOrder.getId());
                //控件不存在
                if (isNullOrEmpty(fileControlConfig)) {
                    return CommonResult.failed(CommonCodeEnum.CONTROL_NOT_EXIST);
                }
                //控件必传
                if (fileControlConfig.getRequired()) {
                    //可作为条件
                    if (Arrays.asList(ConditionControlSubTypeStrArr).contains(fileControlConfig.getSubType())) {
                        nameResp.setName(fileControlConfig.getName());
                        nameResp.setSubType(fileControlConfig.getSubType());
                        //设置条件
                        List<AppConditionList> appConditionList = appConditionListMapper.selectList(new LambdaQueryWrapper<AppConditionList>()
                                .eq(AppConditionList::getControlSubType, fileControlConfig.getSubType()));
                        List<ConditionNameResp> collect = appConditionList.stream().map(appCondition -> {
                            ConditionNameResp conditionNameResp = new ConditionNameResp();
                            BeanUtils.copyProperties(appCondition, conditionNameResp);
                            return conditionNameResp;
                        }).collect(Collectors.toList());
                        nameResp.setConditionList(collect);

                        conditionalControlNameRespList.add(nameResp);
                    }
                }
            }
        }

        //添加提交人条件
        ConditionalControlNameResp nameResp = new ConditionalControlNameResp();
        nameResp.setSort(0);
        nameResp.setName("提交人");
        nameResp.setSubType(CONDITION_SUBTYPE_SUBMITTER);
        //设置条件
        List<AppConditionList> appConditionList = appConditionListMapper.selectList(new LambdaQueryWrapper<AppConditionList>()
                .eq(AppConditionList::getControlSubType, CONDITION_SUBTYPE_SUBMITTER));
        List<ConditionNameResp> collect = appConditionList.stream().map(appCondition -> {
            ConditionNameResp conditionNameResp = new ConditionNameResp();
            BeanUtils.copyProperties(appCondition, conditionNameResp);
            return conditionNameResp;
        }).collect(Collectors.toList());
        nameResp.setConditionList(collect);
        conditionalControlNameRespList.add(nameResp);

        return CommonResult.success(conditionalControlNameRespList, conditionalControlNameRespList.size());
    }

}
